package com.haoshuang.zhouzhoubang.base

import com.haoshuang.zhouzhoubang.model.api.ApiRepo
import com.haoshuang.zhouzhoubang.model.bean.AppVersionBean
import com.haoshuang.zhouzhoubang.model.bean.CallBean
import com.haoshuang.zhouzhoubang.model.bean.DataRankBean
import com.haoshuang.zhouzhoubang.model.bean.DataStatisticalBean
import com.haoshuang.zhouzhoubang.model.bean.MemberBean
import com.haoshuang.zhouzhoubang.model.bean.OrderDetailBean
import com.haoshuang.zhouzhoubang.model.bean.RemoteAddressBean
import com.haoshuang.zhouzhoubang.model.bean.RenWuBean
import com.haoshuang.zhouzhoubang.model.bean.StoreMemberDataStatisticalBean
import com.haoshuang.zhouzhoubang.model.bean.StoreServiceDataStatisticalBean
import com.haoshuang.zhouzhoubang.model.bean.StoreServiceSkillBean
import com.haoshuang.zhouzhoubang.model.bean.YyBean
import com.haoshuang.zhouzhoubang.model.bean.UserBean
import com.mufeng.libs.base.BaseViewModel
import com.mufeng.libs.net.ResponseBean
import com.mufeng.libs.net.netRequest
import com.mufeng.libs.state.StateData
import com.mufeng.libs.state.StatefulMutableLiveData

open class BaseCommonViewModel : BaseViewModel() {

    /**
     * 根据经纬度获取地址
     */
    val getRemoteAddressData = StatefulMutableLiveData<RemoteAddressBean>()
    fun getRemoteAddress(lat: String, lng: String) = netRequest {
        start { getRemoteAddressData.value = StateData.Loading }
        request { ApiRepo.getRemoteAddress(lat, lng) }
        success { getRemoteAddressData.value = StateData.Success(it.data) }
        error { code, msg ->
            getRemoteAddressData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 待确认数据信息
     */
    val confirmOrderTimeData = StatefulMutableLiveData<ResponseBean<YyBean>>()
    fun confirmOrderTime(orderId: String) = netRequest {
        start { confirmOrderTimeData.value = StateData.Loading }
        request { ApiRepo.confirmOrderTime(orderId) }
        success { confirmOrderTimeData.value = StateData.Success(it) }
        error { code, msg ->
            confirmOrderTimeData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 获取订单详情
     */
    val getServiceOrderDetailData = StatefulMutableLiveData<ResponseBean<OrderDetailBean>>()
    fun getServiceOrderDetail(id: String?) = netRequest {
        start { getServiceOrderDetailData.value = StateData.Loading }
        request { ApiRepo.getServiceOrderDetail(id) }
        success { getServiceOrderDetailData.value = StateData.Success(it) }
        error { code, msg ->
            getServiceOrderDetailData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 补偿用户心意金
     */
    val masterCompensateMindAmountData = StatefulMutableLiveData<ResponseBean<Any>>()
    fun masterCompensateMindAmount(orderId: String, mind_amount: String) = netRequest {
        start { masterCompensateMindAmountData.value = StateData.Loading }
        request { ApiRepo.masterCompensateMindAmount(orderId, mind_amount) }
        success { masterCompensateMindAmountData.value = StateData.Success(it) }
        error { code, msg ->
            masterCompensateMindAmountData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 同意返工
     */
    val masterAgreeReworkData = StatefulMutableLiveData<ResponseBean<Any>>()
    fun masterAgreeRework(
        original_id: String,
        service_day: String,
        service_time: String,
        team_leader: String = "",
        team_member: String = ""
    ) = netRequest {
        start { masterAgreeReworkData.value = StateData.Loading }
        request {
            ApiRepo.masterAgreeRework(
                original_id,
                service_day,
                service_time,
                team_leader,
                team_member
            )
        }
        success { masterAgreeReworkData.value = StateData.Success(it) }
        error { code, msg ->
            masterAgreeReworkData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 添加任务指派
     */
    val addAppointData = StatefulMutableLiveData<ResponseBean<Any>>()
    fun addAppoint(id: String, team_leader: String, team_member: String) = netRequest {
        start { addAppointData.value = StateData.Loading }
        request { ApiRepo.addAppoint(id, team_leader, team_member) }
        success { addAppointData.value = StateData.Success(it) }
        error { code, msg ->
            addAppointData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 任务指派页面数据
     */
    val appointDataData = StatefulMutableLiveData<ResponseBean<RenWuBean>>()
    fun appointData(id: String) = netRequest {
        start { appointDataData.value = StateData.Loading }
        request { ApiRepo.appointData(id) }
        success { appointDataData.value = StateData.Success(it) }
        error { code, msg ->
            appointDataData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 获取验证码
     */
    val getCodeData = StatefulMutableLiveData<ResponseBean<Any>>()
    fun getCode(phone: String) = netRequest {
        start { getCodeData.value = StateData.Loading }
        request { ApiRepo.sendSMS(phone, "6") }
        success { getCodeData.value = StateData.Success(it) }
        error { code, msg ->
            getCodeData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 忘记密码
     */
    val forgetData = StatefulMutableLiveData<ResponseBean<Any>>()
    fun forget(phone: String, code: String, password: String) = netRequest {
        start { forgetData.value = StateData.Loading }
        request { ApiRepo.forget(phone, code, password) }
        success { forgetData.value = StateData.Success(it) }
        error { code, msg ->
            forgetData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 获取验证码
     */
    val sendSMSData = StatefulMutableLiveData<ResponseBean<Any>>()
    fun sendSMS(phone: String) = netRequest {
        start { sendSMSData.value = StateData.Loading }
        request { ApiRepo.sendSMS(phone, "7") }
        success { sendSMSData.value = StateData.Success(it) }
        error { code, msg ->
            sendSMSData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 登录
     */
    val loginData = StatefulMutableLiveData<ResponseBean<UserBean>>()
    fun login(phone: String, code: String) = netRequest {
        start { loginData.value = StateData.Loading }
        request { ApiRepo.login(phone, "2", code = code) }
        success { loginData.value = StateData.Success(it) }
        error { code, msg ->
            loginData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 数据统计
     */
    val getDataStatisticalData = StatefulMutableLiveData<ResponseBean<DataStatisticalBean>>()
    fun getDataStatistical(type: Int = 0, start_time: String = "", end_time: String = "") =
        netRequest {
            start { getDataStatisticalData.value = StateData.Loading }
            request { ApiRepo.getDataStatistical(type, start_time, end_time) }
            success { getDataStatisticalData.value = StateData.Success(it) }
            error { code, msg ->
                getDataStatisticalData.value = StateData.Error(code, msg)
            }
        }

    /**
     * 门店下师傅列表
     */
    val getStoreTeamMemberData = StatefulMutableLiveData<ResponseBean<MutableList<MemberBean>>>()
    fun getStoreTeamMember() = netRequest {
        start { getStoreTeamMemberData.value = StateData.Loading }
        request { ApiRepo.getStoreTeamMember() }
        success { getStoreTeamMemberData.value = StateData.Success(it) }
        error { code, msg ->
            getStoreTeamMemberData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 门店下技能列表
     */
    val getStoreServiceSkillData =
        StatefulMutableLiveData<ResponseBean<MutableList<StoreServiceSkillBean>>>()

    fun getStoreServiceSkill() = netRequest {
        start { getStoreServiceSkillData.value = StateData.Loading }
        request { ApiRepo.getStoreService() }
        success { getStoreServiceSkillData.value = StateData.Success(it) }
        error { code, msg ->
            getStoreServiceSkillData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 门店成员统计
     */
    val getMemberStatisticalData =
        StatefulMutableLiveData<ResponseBean<StoreMemberDataStatisticalBean>>()

    fun getMemberStatistical(
        type: Int,
        start_time: String = "",
        end_time: String = "",
        child_id: String = ""
    ) = netRequest {
        start { getMemberStatisticalData.value = StateData.Loading }
        request { ApiRepo.getMemberStatistical(type, start_time, end_time, child_id) }
        success { getMemberStatisticalData.value = StateData.Success(it) }
        error { code, msg ->
            getMemberStatisticalData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 门店服务统计
     */
    val getServiceStatisticalData =
        StatefulMutableLiveData<ResponseBean<StoreServiceDataStatisticalBean>>()

    fun getServiceStatistical(
        type: Int,
        start_time: String = "",
        end_time: String = "",
        service_id: String = ""
    ) = netRequest {
        start { getServiceStatisticalData.value = StateData.Loading }
        request { ApiRepo.getServiceStatistical(type, start_time, end_time, service_id) }
        success { getServiceStatisticalData.value = StateData.Success(it) }
        error { code, msg ->
            getServiceStatisticalData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 获取排行
     */
    val getDataRankData = StatefulMutableLiveData<ResponseBean<MutableList<DataRankBean>>>()
    fun getDataRank(
        type: Int,
        start_time: String = "",
        end_time: String = "",
        sort_type: Int = 1
    ) = netRequest {
        start { getDataRankData.value = StateData.Loading }
        request { ApiRepo.getDataRank(type, start_time, end_time, sort_type) }
        success { getDataRankData.value = StateData.Success(it) }
        error { code, msg ->
            getDataRankData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 获取版本信息
     */
    val getAppVersionsData = StatefulMutableLiveData<AppVersionBean>()
    fun getAppVersions() = netRequest {
        start { getAppVersionsData.value = StateData.Loading }
        request { ApiRepo.getAppVersions()  }
        success { getAppVersionsData.value = StateData.Success(it.data) }
        error { code, msg ->
            getAppVersionsData.value = StateData.Error(code, msg)
        }
    }
}